package com.telenav.app;

import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.telenav.app.widget.ActionBar;
import com.telenav.app.widget.ActionBar.Action;
import com.telenav.app.widget.ActionBar.IntentAction;
import com.telenav.data.dao.AccountDao;
import com.telenav.module.activities.ActivityDetail;
import com.telenav.module.city.CityActivity;
import com.telenav.module.map.SmartMapActivity;
import com.telenav.module.user.FriendListActivity;
import com.telenav.module.user.LoginActivity;
import com.telenav.xmpp.XmppTool;

public class YYMainActivity extends Activity {

	private ViewPager viewPager;

	private ArrayList<View> pageViews;

	private ImageView imageView;

	private TextView pagesText;

	private int currrentItem;

	private ScheduledExecutorService scheduledService;

	private int[] welPictures = { R.drawable.yy_profile_bg_1,
			R.drawable.yy_profile_bg_2, R.drawable.yy_profile_bg_3,
			R.drawable.yy_profile_bg_4, R.drawable.yy_profile_bg_5 };

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yy_main);

		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle("精彩推荐");

		final Action otherAction = new IntentAction(this, new Intent(this,
				CityActivity.class), R.drawable.yy_action_bar_search);
		actionBar.addRightAction(otherAction);

		pageViews = new ArrayList<View>();
		for (int i = 0; i < welPictures.length; i++) {
			imageView = new ImageView(YYMainActivity.this);
			imageView.setLayoutParams(new LayoutParams(10, 10));
			imageView.setPadding(10, 0, 10, 0);
			imageView.setBackgroundResource(welPictures[i]);
			imageView.setOnClickListener(new ImagePageClickListener());
			pageViews.add(imageView);
		}

		viewPager = (ViewPager) findViewById(R.id.main_image_pager);
		viewPager.setAdapter(new ImagePageAdapter());
		viewPager.setOnPageChangeListener(new ImagePageChangeListener());

		pagesText = (TextView) findViewById(R.id.main_pages_text);
		pagesText.setText((currrentItem + 1) + "/" + pageViews.size());

		RelativeLayout messageLayout = (RelativeLayout) findViewById(R.id.main_my_messages);
		messageLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (AccountDao.getInstance().isLogin()) {
					Intent intent = new Intent(YYMainActivity.this,
							FriendListActivity.class);
					startActivity(intent);
				} else {
					Intent intent = new Intent(YYMainActivity.this,
							LoginActivity.class);
					startActivity(intent);
				}
			}

		});

		RelativeLayout nearLayout = (RelativeLayout) findViewById(R.id.main_near_activities);
		nearLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(YYMainActivity.this, SmartMapActivity.class);
				startActivity(intent);
			}

		});
	}

	@Override
	protected void onStart() {
		super.onStart();
		scheduledService = Executors.newSingleThreadScheduledExecutor();
		scheduledService.scheduleAtFixedRate(new Runnable() {
			public void run() {
				currrentItem = (currrentItem + 1) % pageViews.size();
				handler.sendEmptyMessage(0);
			}
		}, 10, 5, TimeUnit.SECONDS);
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			viewPager.setCurrentItem(currrentItem);
		}
	};

	@Override
	protected void onStop() {
		super.onStop();
		scheduledService.shutdown();
	}

	class ImagePageAdapter extends PagerAdapter {

		@Override
		public void destroyItem(View v, int position, Object arg2) {
			// TODO Auto-generated method stub
			((ViewPager) v).removeView(pageViews.get(position));
		}

		@Override
		public void finishUpdate(View arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return pageViews.size();
		}

		@Override
		public Object instantiateItem(View v, int position) {
			// TODO Auto-generated method stub
			((ViewPager) v).addView(pageViews.get(position));
			return pageViews.get(position);
		}

		@Override
		public boolean isViewFromObject(View v, Object arg1) {
			// TODO Auto-generated method stub
			return v == arg1;
		}

		@Override
		public void startUpdate(View arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			// TODO Auto-generated method stub
		}

		@Override
		public Parcelable saveState() {
			// TODO Auto-generated method stub
			return null;
		}
	}

	class ImagePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			currrentItem = position % pageViews.size();
			pagesText.setText((currrentItem + 1) + "/" + pageViews.size());
			viewPager.setCurrentItem(position);
		}

	}

	class ImagePageClickListener implements OnClickListener {
		@Override
		public void onClick(View paramView) {
			// TODO Auto-generated method stub
			System.out.println("YYMainActivity.ImagePageClickListener.onClick()" + currrentItem);
			Intent intent = new Intent(YYMainActivity.this,
					ActivityDetail.class);
			startActivity(intent);
		}

	}
}
